package br.unifor.cct.mia.initialize;

import java.awt.Point;
import java.util.List;

import br.unifor.cct.mia.dataenhancement.Attribute;
import br.unifor.cct.mia.dataenhancement.Structure;
import br.unifor.cct.mia.evolutionary.Genotype;
import br.unifor.cct.mia.ga.Ga;
import br.unifor.cct.mia.util.Methods;

public class RepeatableRandomInitialize implements Initialize {

	public Object execute(Object value) {
		Object[] o = (Object[]) value;
		List positions = (List) o[0];
		Structure structure = (Structure) o[1];
		Ga ga = (Ga)o[2];
		Integer specieCount = (Integer)o[3];
				
		ga.population = new Genotype[ga.configurations.getPopsize() + 1];
        
    	int inicio = specieCount.intValue()*ga.configurations.getNvars();
    	List positionsResized = positions.subList(inicio,inicio+ga.configurations.getNvars()); 
		
		
        for (int i = 0; i < ga.configurations.getPopsize()+1; i++) {
        	ga.population[i] = new Genotype(ga.configurations);
        	
        	ga.population[i].setFitness(0);
        	ga.population[i].setSelectProbability(0);   
        	ga.population[i].setSpeciesCount(specieCount.intValue());
            
            for (int j = 0; j < ga.configurations.getNvars(); j++) {
            	Point p = (Point)positionsResized.get(j);
            	Attribute att = structure.getAttribute(p.y);
           	
            	((Genotype)ga.population[i]).setLower(att.getMin(), j);
            	((Genotype)ga.population[i]).setUpper(att.getMax(), j); 	       
            	((Genotype)ga.population[i]).setInteger(att.isInteger(), j);
            	
            	if (att.isInteger())
            		((Genotype)ga.population[i]).setGene(Methods.randIntValue((int)att.getMin(), (int)att.getMax()), j);
            	else
            		((Genotype)ga.population[i]).setGene(Methods.randDoubleValue(att.getMin(), att.getMax()), j);
            }
        }
	        
		return ga.population;
	}
}